package com.freshcity.page.talk
{
	import com.freshcity.common.IsLimited;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.HBox;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.graphics.MessageStatus;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.utils.StringUtil;
	import com.pblabs.debug.Logger;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class UserDetailPanel extends Component
	{
		[Embed(source='../assets/level-1.png')]
		public var Level1Img:Class;
		[Embed(source='../assets/level-2.png')]
		public var Level2Img:Class;
		[Embed(source='../assets/level-3.png')]
		public var Level3Img:Class;

		private var user:*;
		private var report:*;
		private var mine:*;
		private var currentY:int=5;
		private var posX:int=5;
		private var isLimitText:TextField;

		public function UserDetailPanel(user:*, report:*)
		{
			this.user=user;
			this.report=report;
			this.mine=GlobalVar.LOGIN_USER;
			this.mouseEnabled=false;
			super(null, 0, 0);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			renderContent();
			addBottomPadding();
			this.height = this.currentY+8;
		}

		override public function destroy():void
		{
			if (isLimitText)
			{
				isLimitText.removeEventListener(MouseEvent.CLICK, isLimitTextCallback);
			}
			super.destroy();
		}

		//添加留空
		private function addBottomPadding():void
		{
			var shape:Shape=new Shape();
			shape.graphics.beginFill(0, 0);
			shape.graphics.drawRect(0, 0, width, 5);
			shape.graphics.endFill();
			addChild(shape);
		}

		private function renderContent():void
		{
			var i:int=user.level - 3 <= 0 ? 0 : (user.level - 3);
			renderLevel(user.level);

			var userNo:String="".concat(user.id, "（", user.level, "级）");
			createProperty("用户", userNo);
			if (user.sex)
			{
				var l:String="性别";
				var v:String=user.sex == "female" ? "女" : "男";
				var s:int=(report && report.sex) ? report.sex : 0;
				createProperty(l, v);
			}
			setProperties();
		}

		private function setProperties():void
		{
			var len:int=GlobalVar.nameProperty.length;
			for (var i:int=0; i < len; i++)
			{
				var s1:String=GlobalVar.nameProperty[i];
				var s2:String=GlobalVar.labelProperty[i];
				if (mine[s1])
				{
					createProperty(s2, user[s1]);
				}
			}

			if (user.level < mine.level)
			{
				isLimitText = new TextField();
				isLimitText.autoSize=TextFieldAutoSize.LEFT;
				//isLimitText.width=240;
				//isLimitText.multiline=true;
				//isLimitText.wordWrap=true;
				var myCSS:StyleSheet=new StyleSheet();
				myCSS.setStyle("a:link", {color: '#0000ff', textDecoration: 'none'});
				myCSS.setStyle("a:hover", {color: '#ff0000', textDecoration: 'underline'});
				isLimitText.styleSheet=myCSS;
				isLimitText.htmlText=""+"<a href='event:'>" + ">>" + "对方信用级别太低，邀请TA升级信用</a>";
				isLimitText.addEventListener(MouseEvent.CLICK, isLimitTextCallback);

				//isLimitIcon.width=24;
				//isLimitIcon.height=24;
				isLimitText.x=5;
				isLimitText.y=this.currentY;
				this.currentY+=25;
				addChild(isLimitText);
			}
		}
		
		protected function isLimitTextCallback(event:Event):void
		{
			if(mine.level>user.level){
				Logger.debug(this,"opcodes","REQ_LEVEL_UP:"+OpCodes.REQ_LEVEL_UP);
				main.commu.send(2310, {to: user.id}, onReqLevelUpCallBack);
			}
		}
		
		private function onReqLevelUpCallBack(data:*):void
		{
			main.msg.showRollTipsMsg("信息已发出");
			isLimitText.visible=false;
		}

		private function renderLevel(level:int):void
		{
			/*
			var box:HBox = new HBox();
			box.spacing = 0;
			var level3:int = level/13;
			for (var i:int = 0; i < level3; i++)
			{
				box.addChild(new Level3Img());
			}
			level -= level3*13;
			var level2:int = level/4;
			for (var j:int = 0; j < level2; j++)
			{
				box.addChild(new Level2Img());
			}
			level -= level2*4;
			var level1:int = level;
			for (var x:int = 0; x < level1; x++)
			{
				box.addChild(new Level1Img());
			}
			box.initialize();
			*/

			var box:TextField=getTxt("".concat("级别", ":", String(level)));
			box.x=this.posX;
			box.y=this.currentY;
			//addChild(box);
			//this.currentY += 20;
		}

		private function createProperty(label:String, value:String):void
		{
			if (!value)
				return;

			var box:HBox=new HBox();
			box.alignment=HBox.MIDDLE;

			var l:TextField=getTxt("".concat(StringUtil.addColor(label + " ", "#666666"), value));
			box.addChild(l);
			box.initialize();

			box.x=this.posX;
			box.y=this.currentY;
			this.currentY+=20;

			addChild(box);
		}

		private function getTxt(content:String):TextField
		{
			var tf:TextField=new TextField();
			tf.defaultTextFormat=new TextFormat("宋体", 12);
			tf.autoSize=TextFormatAlign.LEFT;
			tf.height=20;
			tf.width=180;
			tf.htmlText=content;
			return tf;
		}
	}
}
